package com.ysuedu.servlet;

import com.ysuedu.common.SetCode;
import com.ysuedu.entity.User;
import com.ysuedu.service.UserService;
import com.ysuedu.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "UserLoginServlet",urlPatterns = "/User/LoginServlet")
public class UserLoginServlet extends HttpServlet {

    @Override
    public void init() throws ServletException {
        super.init();
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        SetCode.setEncoding(request,response);
        String userAccount=request.getParameter("userAccount");
        String password=request.getParameter("userPassword");
        UserService userService=new UserServiceImpl();
        System.out.print(userAccount+":"+password);
        User user=userService.login(userAccount,password);
        PrintWriter writer=response.getWriter();
        request.setAttribute("user",user);
        if(null!=user){
            writer.print("1");
        }else{
            writer.print("0");
        }
        writer.close();

    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        SetCode.setEncoding(request,response);
    }
}
